import subprocess
import os
import fileinput
import sys
#import argparse
from optparse import OptionParser

def main(argv=None):
  # handle some args: name of binary file;
  if argv is None:
    argv = sys.argv 

#  if Python V2.7 or newer
#  parser = argparse.ArgumentParser(description='Process average...')
#  parser.add_argument('--name', '-n', required=True, help='Name of the binary file to be executed...')
#  parser.add_argument('--repeat_times', '-r', type=int, default=10, help='repeat times to execute the binary...')
#  args = parser.parse_args()

# if older than Python 2.7
  parser = OptionParser()
  parser.add_option('--name', '-n', help='Name of the binary file to be executed...')
  parser.add_option('--repeat_times', '-r', type=int, default=10, help='repeat times to execute the binary...')
  (options, args) = parser.parse_args()

  repeat_time = int(options.repeat_times)
  name = options.name
	
  # create output file
  output_file = name + "_output.txt"
  fo = open(output_file, "w")
  
  # launch binary multiple times
  for i in range(repeat_time):
    subprocess.call("./" + name, stdout=fo)

  # calc total and average
  sum_tot = 0
  for line in fileinput.input(output_file):
    sum_tot += float(line)

  average = sum_tot / float(repeat_time)
  print "Total is %f" %sum_tot
  print "Average is %3.8f" %average 

if __name__ == "__main__":
  main()
    
